// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.internal.services;

import lichen.services.Encoder;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.tapestry5.ioc.internal.util.Defense;

/**
 * encoder的实现.
 * @author jcai
 * @version $Revision: 225 $
 * @since 0.0.3
 */
public class EncoderImpl implements Encoder{

	/**
	 * 
	 * @see lichen.services.Encoder#decodUrl(java.lang.String)
	 * @since 0.0.3
	 */
	public String decodUrl(String securedUrl) {
		Defense.notNull(securedUrl, "secured url");
		return new String(Base64.decodeBase64(securedUrl.getBytes()));
	}

	/**
	 * 
	 * @see lichen.services.Encoder#encodPassword(java.lang.String)
	 * @since 0.0.3
	 */
	public String encodPassword(String plainPassword) {
		Defense.notNull(plainPassword, "plain password");
		return DigestUtils.md5Hex(plainPassword);
	}

	/**
	 * 
	 * @see lichen.services.Encoder#encodUrl(java.lang.String)
	 * @since 0.0.3
	 */
	public String encodUrl(String planUrl) {
		Defense.notNull(planUrl, "plan url");
		return new String(Base64.encodeBase64(planUrl.getBytes()));
	}
}
